/*-
 * Copyright 2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __array_h__
#define __array_h__
//-----------------------------------------------------------------------------
#include "config.h"
#include "rand.h"
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
typedef struct array {
  uint8_t * a;
  uintptr_t count;           // elements in array
  uint32_t reserve;         // elements
  uint32_t reserved;        // elements
  uint32_t element_size;    // element size
  uint32_t static_data_size;
  // statistic for adaptive sizing
  uint32_t resize_count;
  uint32_t copy_count;

  union {
    void * element_init;
    void (* element_initf)(void * data,void * object);
  };
  union {
    void * element_destroy;
    void (* element_destroyf)(void * data,void * object);
  };
  union {
    void * element_copy;
    void (* element_copyf)(void * dst,const void * src,void * object);
  };
  union {
    void * element_compare;
    intptr_t (* element_comparef)(const void * data,const void * data_in_array,void * object);
  };
  union {
    void * element_xchg;
    void (* element_xchgf)(void * element1,void * element2,void * temp_element,void * object);
  };

  void * object;

} array;
//---------------------------------------------------------------------------
array * array_new(void);
void array_init(array * ar);
void array_init_copy(array * ar,const array * src,uintptr_t static_data_size /*= 0*/);
void array_destroy(array * ar);
void array_delete(array * ar);
void array_copy(array * ar,const array * src);
void array_resize(array * ar,uintptr_t n);
void array_set_element_size(array * ar,uintptr_t element_size,uintptr_t static_data_size /*= 0*/);
//---------------------------------------------------------------------------
void array_append(array * ar);
void array_append_copy(array * ar,const void * data);
void array_insert(array * ar,uintptr_t idx);
void array_insert_copy(array * ar,uintptr_t idx,const void * data);
void array_remove(array * ar,uintptr_t idx);
void array_copy_paste(
  array * ar,
  uintptr_t dst_position,
  const array * src,
  uintptr_t src_position,
  uintptr_t count);
//---------------------------------------------------------------------------
intptr_t array_lookup(const array * ar,const void * data,uintptr_t * insert_position);
intptr_t array_lookupf(const array * ar,const void * data,uintptr_t * insert_position,void * element_comparef);
intptr_t array_lookup_insert_copy(array * ar,const void * data);
void array_sort(array * ar);
void array_shuffle(array * ar,struct rand * rnd,uintptr_t count);
//---------------------------------------------------------------------------
static __inline__ void * array_get(const array * ar,uintptr_t idx)
{
#if COMPILE_RUNTIME_CHECKS
  if( idx >= ar->count ){
    fprintf(stderr,"undefined behavior in %s, line %d\n",__FILE__,__LINE__);
    abort();
  }
#endif
  return (uint8_t *) ar->a + idx * ar->element_size;
}
//---------------------------------------------------------------------------
static __inline__ void array_set(array * ar,uintptr_t idx,const void * element)
{
#if COMPILE_RUNTIME_CHECKS
  if( idx >= ar->count ){
    fprintf(stderr,"undefined behavior in %s, line %d\n",__FILE__,__LINE__);
    abort();
  }
#endif
  if( ar->element_copy != NULL )
    ar->element_copyf((uint8_t *) ar->a + idx * ar->element_size,element,ar->object);
  else
    memcpy((uint8_t *) ar->a + idx * ar->element_size,element,ar->element_size);
}
//---------------------------------------------------------------------------
static __inline__ void * array_append_get(array * ar)
{
  uintptr_t idx = ar->count;
  array_resize(ar,idx + 1);
#if COMPILE_RUNTIME_CHECKS
  if( idx >= ar->count ){
    fprintf(stderr,"undefined behavior in %s, line %d\n",__FILE__,__LINE__);
    abort();
  }
#endif
  return (uint8_t *) ar->a + idx * ar->element_size;
}
//---------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
